package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.QuestionOptionMapper;
import com.one.group.model.entity.QuestionOption;
import com.one.group.service.QuestionOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class QuestionOptionServiceImpl extends ServiceImpl<QuestionOptionMapper, QuestionOption> implements QuestionOptionService {

    private Long getCurrentTenantId() {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return tenantId;
    }
    @Override
    public boolean addOption(QuestionOption option) {
        Long tenantId = getCurrentTenantId() ;
        option.setTenantId(tenantId);
        boolean result = getMapper().addOption(option);
        if (result) {
            log.debug("添加问题选项成功，选项ID: {}, 问题ID: {}, 选项内容: {}", option.getId(), option.getQuestionId(), option.getContent());
        } else {
            log.debug("添加问题选项失败，选项内容: {}", option.getContent());
        }
        return  result;
    }

    @Override
    public boolean deleteOption(Long optionId) {
        Long tenantId = getCurrentTenantId();

        Map map = new HashMap();
        map.put("optionId", optionId);
        map.put("tenantId", tenantId);
               getMapper().deleteOption(map);
        return false;
    }

    @Override
    public boolean deleteOptionsByQuestionId(Long questionId) {
        Long tenantId = getCurrentTenantId();

        Map map=new HashMap();
        map.put("questionId", questionId);
        map.put("tenantId", tenantId);
                getMapper().deleteOptionsByQuestionId(map);
        return false;
    }

    @Override
    public List<QuestionOption> getOptionsByQuestionId(Long questionId) {
        Long tenantId = getCurrentTenantId();

        Map map=new HashMap();
        map.put("questionId", questionId);
        map.put("tenantId", tenantId);
        List<QuestionOption> options = getMapper().getOptionsByQuestionId(map);
                return options;

    }
    @Override
    public boolean updateOption(QuestionOption option) {
        Long tenantId = getCurrentTenantId();
        option.setTenantId(tenantId);
        boolean result = getMapper().updateOption(option);
        if (result) {
            log.debug("更新问题选项成功，选项ID: {}, 问题ID: {}, 选项内容: {}", option.getId(), option.getQuestionId(), option.getContent());
        } else {
            log.debug("更新问题选项失败，选项ID: {}, 问题ID: {}, 选项内容: {}", option.getId(), option.getQuestionId(), option.getContent());
        }
        return result;
    }

    @Override
    public boolean batchUpdateSort(Long questionId) {
        if (questionId == null) {
            log.debug("根据问题ID排序失败：问题ID为空");
            return false;
        }
        
        Long tenantId = getCurrentTenantId();
        log.debug("开始根据问题ID对选项进行重新排序，问题ID: {}, 租户ID: {}", questionId, tenantId);
        
        boolean result = getMapper().batchUpdateSort(questionId, tenantId);
        if (result) {
            log.debug("根据问题ID对选项重新排序成功");
        } else {
            log.debug("根据问题ID对选项重新排序失败");
        }
        return result;
    }
}
